//table头信息 不变
var gh_columnData = [
    {"Width": "10%", "ColumnName": "客户Id"},
    {"Width": "10%", "ColumnName": "客户名称"},
    {"Width": "10%", "ColumnName": "责任人"},
    {"Width": "10%", "ColumnName": "客户类型"},
    {"Width": "10%", "ColumnName": "客户等级"},
    {"Width": "10%", "ColumnName": "联系人"},
    {"Width": "10%", "ColumnName": "手机号"},
    {"Width": "10%", "ColumnName": "来源"},
    {"Width": "10%", "ColumnName": "录入时间"},
    {"Width": "10%", "ColumnName": "操作"}
];

//操作按钮 不变
var gh_operateHtml = "<div class='form-inline'>" +
    " <button type='button' class='btn btn-primary innerButtons'  value='claim'>认领</button>" +
    " <button type='button' class='btn btn-primary innerButtons'  value='view'>查看</button>" +
    "</div>";


//用户管理分页函数
var gotoPage = function (index) {
    //TODO url
    var url = webPath + "/crm/1";
    var postData = $("#m-gonghaiForm").serialize();
    var options = {"passArguments": index}
    postAjax(url, postData, gotoPageSuccess, options);

};

var gotoPageSuccess = function (result, passArguments) {
    //TODO remove
    var result = '{"totalNumber":3,"list":[{"customerId":"22", "customerName":"张三", "customerGrade":"潜在客户",' +
        '"idNumber":"333333333333333333","recordTime":"2015-6-9","contact":"李四",' +
        '"responsiblePerson":"责任人","customerType":"生产企业","source":"网站注册","phoneNumber":"111111111",' +
        '"industry":"钢铁"}]}';

    var columnList = ["customerId", "customerName", "responsiblePerson", "customerGrade", "customerType", "contact", "phoneNumber", "source", "recordTime"];
    //$.post(url, postData, function (result) {
    cleanTableAndPage("gonghaiCustomerInfoTable");
    var jsonData = $.parseJSON(result);
    //画table
    $("#gonghaiCustomerInfoTable").MakeTable(false, gh_columnData, jsonData.list, tableClass, columnList, gh_operateHtml);
    //画分页
    $("#gonghaiCustomerInfoTable").after(comPagination(passArguments, pageSize, jsonData.totalNumber, "gotoPage", "gonghaiCustomerInfoP"));

    //Table中操作栏 认领 查看
    $("#gonghaiCustomerInfoTable button").click(function () {
        var that = $(this);
        var customerId = getCustomerId(that);
        var buttonType = that.val();
        that.val() === "claim" ?
            claim(that) :
            that.val() === "view" ? getPage(webPath + "/crm/getViewRecords/" + customerId + "/0.do") :
                alert("something wrong");
    });
    //});
}

var claim = function(that) {
    showConfirm("你确定要认领这条客户信息吗？", function(){
        claimInfo(that);
        ;
    });
}

var claimInfo = function(that){
    var url = "";
    var postData = {customerId:getCustomerId(that)}
    postAjax(url,postData,function(){gotoPage(1)});
}



////查询按钮行为 TODO Change
//$('#searchTrackBtn').click(function () {
//    gotoPage(1);
//});

//获取id 方便提交 TODO Change
var getCustomerId = function (that) {
    return that.closest("tr").find("td div")[1].innerHTML;
}